#include "assignment_utils.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
#include "car.h"
// 保存所有Car及学生信息到txt文件
void saveToTxt(const std::vector<Car>& cars, const std::string& filename) {
    std::ofstream ofs(filename);
    for (const auto& c : cars) {
        ofs << c.id << '|'
            << c.chassis.id << '|'
            << c.chassis.model << '|'
            << c.chassis.wheelbase << '|'
            << c.chassis.track << '|'
            << c.chassis.min_clearance << '|'
            << c.chassis.min_turn_radius << '|'
            << c.chassis.drive_type << '|'
            << c.chassis.max_range << '|';
        for (int i = 0; i < 4; ++i) {
            ofs << c.chassis.tires[i].type << ',' << c.chassis.tires[i].size;
            if (i != 3) ofs << '/';
        }
        ofs << '|'
            << c.agx.model << '|'
            << c.agx.ai << '|'
            << c.agx.cuda_cores << '|'
            << c.agx.tensor_cores << '|'
            << c.agx.vram << '|'
            << c.agx.storage << '|'
            << c.camera.model << '|'
            << c.camera.camera << '|'
            << c.camera.rgb_resolution << '|'
            << c.camera.rgb_fps << '|'
            << c.camera.fov << '|'
            << c.camera.depth_fps << '|'
            << c.lidar.model << '|'
            << c.lidar.channels << '|'
            << c.lidar.range << '|'
            << c.lidar.power << '|'
            << c.gyro.model << '|'
            << c.gyro.manufacturer << '|'
            << c.lcd.size << '|'
            << c.lcd.model << '|'
            << c.battery.param << '|'
            << c.battery.output << '|'
            << c.battery.charge_time << '|'
            << c.assignedStudent.id << '|'
            << c.assignedStudent.name << '\n';
    }
}

// 从txt文件读取所有Car及学生信息
void loadFromTxt(std::vector<Car>& cars, const std::string& filename) {
    std::ifstream ifs(filename);
    if (!ifs) return;
    cars.clear();
    std::string line;
    while (std::getline(ifs, line)) {
        std::istringstream iss(line);
        Car c;
        std::string field;
        std::getline(iss, c.id, '|');
        std::getline(iss, c.chassis.id, '|');
        std::getline(iss, c.chassis.model, '|');
        std::getline(iss, c.chassis.wheelbase, '|');
        std::getline(iss, c.chassis.track, '|');
        std::getline(iss, c.chassis.min_clearance, '|');
        std::getline(iss, c.chassis.min_turn_radius, '|');
        std::getline(iss, c.chassis.drive_type, '|');
        std::getline(iss, c.chassis.max_range, '|');
        std::getline(iss, field, '|'); // 轮胎
        c.chassis.tires.clear();
        std::istringstream tire_ss(field);
        for (int i = 0; i < 4; ++i) {
            std::string tire;
            std::getline(tire_ss, tire, i==3 ? '\0' : '/');
            auto pos = tire.find(',');
            Tire t;
            t.type = tire.substr(0, pos);
            t.size = tire.substr(pos+1);
            c.chassis.tires.push_back(t);
        }
        std::getline(iss, c.agx.model, '|');
        std::getline(iss, c.agx.ai, '|');
        iss >> c.agx.cuda_cores; iss.get();
        iss >> c.agx.tensor_cores; iss.get();
        std::getline(iss, c.agx.vram, '|');
        std::getline(iss, c.agx.storage, '|');
        std::getline(iss, c.camera.model, '|');
        std::getline(iss, c.camera.camera, '|');
        std::getline(iss, c.camera.rgb_resolution, '|');
        iss >> c.camera.rgb_fps; iss.get();
        std::getline(iss, c.camera.fov, '|');
        iss >> c.camera.depth_fps; iss.get();
        std::getline(iss, c.lidar.model, '|');
        iss >> c.lidar.channels; iss.get();
        std::getline(iss, c.lidar.range, '|');
        std::getline(iss, c.lidar.power, '|');
        std::getline(iss, c.gyro.model, '|');
        std::getline(iss, c.gyro.manufacturer, '|');
        std::getline(iss, c.lcd.size, '|');
        std::getline(iss, c.lcd.model, '|');
        std::getline(iss, c.battery.param, '|');
        std::getline(iss, c.battery.output, '|');
        std::getline(iss, c.battery.charge_time, '|');
        std::getline(iss, c.assignedStudent.id, '|');
        std::getline(iss, c.assignedStudent.name, '\n');
        cars.push_back(c);
    }
}
void showCars(const std::vector<Car>& cars) {
    int idx = 0;
    while (true) {
        const Car& c = cars[idx];
        std::cout << "\n--- 第" << idx+1 << "辆智能小车信息 ---\n";
        std::cout << "编号: " << c.id << "\n";
        std::cout << "底盘编号: " << c.chassis.id << " 型号: " << c.chassis.model << " 轴距: " << c.chassis.wheelbase << " 轮距: " << c.chassis.track << "\n";
        std::cout << "最小离地间隙: " << c.chassis.min_clearance << " 最小转弯半径: " << c.chassis.min_turn_radius << " 驱动形式: " << c.chassis.drive_type << " 最大行程: " << c.chassis.max_range << "\n";
        std::cout << "轮胎: ";
        for (const auto& t : c.chassis.tires) std::cout << t.type << "(" << t.size << ") ";
        std::cout << "\nAGX套件: " << c.agx.model << " AI: " << c.agx.ai << " CUDA核心: " << c.agx.cuda_cores << " Tensor CORE: " << c.agx.tensor_cores << " 显存: " << c.agx.vram << " 存储: " << c.agx.storage << "\n";
        std::cout << "摄像头: " << c.camera.model << " 摄像头: " << c.camera.camera << " RGB分辨率: " << c.camera.rgb_resolution << " 帧率: " << c.camera.rgb_fps << " FOV: " << c.camera.fov << " 深度帧率: " << c.camera.depth_fps << "\n";
        std::cout << "激光雷达: " << c.lidar.model << " 通道数: " << c.lidar.channels << " 测试范围: " << c.lidar.range << " 功耗: " << c.lidar.power << "\n";
        std::cout << "陀螺仪: " << c.gyro.model << " 厂家: " << c.gyro.manufacturer << "\n";
        std::cout << "液晶显示屏: " << c.lcd.size << "寸 型号: " << c.lcd.model << "\n";
        std::cout << "电池模块: " << c.battery.param << " 对外供电: " << c.battery.output << " 充电时长: " << c.battery.charge_time << "\n";
        std::cout << "分配学生: " << c.assignedStudent.id << " " << c.assignedStudent.name << "\n";
        std::cout << "[n]下一辆 [p]上一辆 [q]退出\n";
        std::cout << "请输入操作(n/p/q)后回车: ";
        char ch;
        std::cin >> ch;
        if (ch == 'n' && idx < (int)cars.size()-1) ++idx;
        else if (ch == 'p' && idx > 0) --idx;
        else if (ch == 'q') break;
    }
}
